package it.devapp.gridview;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;

public class MainActivity extends Activity
{
	private GridView grid;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		grid = (GridView) findViewById(R.id.grid);
		aggiornaProvince(Provincia.TUTTE_LE_PROVINCE);

		Spinner spinner = (Spinner) findViewById(R.id.spinner);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, Provincia.getRegioni());
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);

		spinner.setOnItemSelectedListener(new OnItemSelectedListener()
		{
			@Override
			public void onItemSelected(AdapterView<?> adapter, View v, int pos, long id)
			{
				aggiornaProvince((String) adapter.getItemAtPosition(pos));
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0)
			{
				aggiornaProvince(Provincia.TUTTE_LE_PROVINCE);
			}
		});
	}

	protected void aggiornaProvince(String regione)
	{
		grid.setAdapter(new SimpleAdapter(this, Provincia.getData(regione), R.layout.cell, new String[]
		{ Provincia.CODICE, Provincia.NOME }, new int[]
		{ R.id.codice, R.id.nome }));
	}
}